//
// Created by 34649 on 2022/3/4.
//

#include "http_response.h"
using namespace http;


http_response::http_response():httpVersion(HTTP_VERSION::HTTP_11) ,httpCode(HTTP_CODE::OK){
    _map["Server"]="ayin";
}

string http_response::serialize() {
    string res;
    //响应行
    res+=(versionMap.at(httpVersion));
    res+=' ';
    res+=std::to_string(static_cast<int>(httpCode));
    res+=' ';
    //if(httpCode)
    res+=(httpCodeMap.at(httpCode));
    res+=CRLF;
    //header
    for(auto [key,value]:_map){
        string line=key+": "+value+CRLF;
        res+=line;
    }
    res+=CRLF;
    //content
    res+=_content;
    return res;
}

bool http_response::setHttpVersion(HTTP_VERSION version) {
    this->httpVersion = version;
    return true;
}

bool http_response::setContent(string content) {
    _content=content;
    if(_content.size()!=0){//自动更新Content-Length和Content-Type值;
        _map["Content-Length"]= std::to_string(_content.size());
        _map["Content-Type"]="text/html";
    }
    return true;
}

bool http_response::setHeader(string key, string value) {
    _map[key]=value;
    return true;
}


bool http_response::setHttpCode(HTTP_CODE hc) {
    httpCode=hc;
    return true;
}
